import api from '@/api'
import { deepCopy, setPage } from '@/utils/util'

const initPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 0,
    filter: {
        enabled: null,
        title: null
    }
}

// state
const state = {
    data: {
        departements: []
    },

    ui: {
        page: deepCopy(initPage)
    }
}

// getter
const getters = {
    departements: state => state.data.departements
}

// actions
const actions = {

    getDepartements({ commit }, params) {
        
        api.getDepartements({
            loading: true,
            method: 'get',
            data: params ? params : state.ui.page,
            success: (res) => {
                console.log(res)
                const departements = res
                commit('SETDEPARTEMENTS', {
                    departements: departements
                })
            }
        })
    },

    addDepartement({ dispatch }, params) {
        api.addDepartement({
            loading: true,
            method: 'put',
            data: params,
            success: (res) => {
                dispatch('getDepartements')
            }
        })
    },

    editDepartement({ dispatch }, params) {
        api.editDepartement({
            loading: true,
            method: 'put',
            data: params,
            success: (res) => {
                dispatch('getDepartements')
            }
        })
    },

    removeDepartement({ dispatch }, params) {
        api.removeDepartement({
            loading: true,
            method: 'post',
            data: params,
            success: (res) => {
                dispatch('getDepartements')
            }
        })
    }
}

// mutations
const mutations = {

    SETDEPARTEMENTS(state, payload) {
        state.data.departements = payload.departements
    }

}

export default {
    state,
    getters,
    actions,
    mutations
}